package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    public DeptService deptService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list", name = "查询所有用户数据")
    public String fandAll(@RequestParam(defaultValue = "1", name = "page") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<User> page = userService.findPage(getCompanyId(), pageNum, pageSize);
        request.setAttribute("page", page);
        return "system/user/user-list";
    }

    @RequestMapping(value = "/toAdd", name = "进入到添加用户的页面")
    public String toAdd() {
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList", deptList);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/edit", name = "保存用户数据")
    public String edit(User user) {
        if (StringUtils.isEmpty(user.getId())) {
            user.setId(UUID.randomUUID().toString());
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            userService.insert(user);
        } else {
            String password = user.getPassword();
            if (!StringUtils.isEmpty(password)) {
                password = new Md5Hash(password, user.getEmail(), 2).toString();
                user.setPassword(password);
            }
            user.setUpdateTime(new Date());
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "进入修改用户页面")
    public String toUpdate(String id) {
        User user = userService.findById(id);
        request.setAttribute("user", user);
        List<Dept> list = deptService.findAll(getCompanyId());
        request.setAttribute("deptList", list);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/delete", name = "删除用户方法")
    public String delete(String id) {
        userService.deleteById(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/roleList", name = "进入给用户分配角色页面")
    public String roleList(String id) {
        User user = userService.findById(id);
        request.setAttribute("user", user);
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList", roleList);
        List<String> list = userService.findRoleIdsByUserId(id);
        StringBuffer sb = new StringBuffer("");
        for (String s : list) {
            sb.append(s).append(",");
        }
        request.setAttribute("userRoleStr", sb);
        return "system/user/user-role";
    }

    @RequestMapping(value = "/changeRole", name = "保存用户角色中间表数据")
    public String chengRole(String userid, String[] roleIds) {
        userService.changeRole(userid, roleIds);
        return "redirect:/system/user/list.do";
    }
}
